package com.authine.cloudpivot.util;

import com.aliyun.tea.TeaException;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.taobao.api.ApiException;

import java.util.List;

public class DingDingUtil {






    public static String getToken(String appKey,String appSecret){
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.setAppkey(appKey);
            request.setAppsecret(appSecret);
            request.setHttpMethod("GET");
            OapiGettokenResponse response = client.execute(request);
            return response.getAccessToken();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static com.aliyun.dingtalkmicro_app_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkmicro_app_1_0.Client(config);
    }


    public static void Appscopes(String appKey, String appSecret, String agentId, List<String> userIds) throws Exception {
        com.aliyun.dingtalkmicro_app_1_0.Client client = createClient();
        com.aliyun.dingtalkmicro_app_1_0.models.SetMicroAppScopeHeaders setMicroAppScopeHeaders = new com.aliyun.dingtalkmicro_app_1_0.models.SetMicroAppScopeHeaders();
        setMicroAppScopeHeaders.xAcsDingtalkAccessToken = getToken(appKey,appSecret);
        com.aliyun.dingtalkmicro_app_1_0.models.SetMicroAppScopeRequest setMicroAppScopeRequest = new com.aliyun.dingtalkmicro_app_1_0.models.SetMicroAppScopeRequest()
                .setDelUserIds(userIds
                );
        try {
            client.setMicroAppScopeWithOptions(agentId, setMicroAppScopeRequest, setMicroAppScopeHeaders, new com.aliyun.teautil.models.RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        }
    }

}
